<template>
	<view class="adup-jybz page-bg">
		<view class="fm-title">
			<view class="title"><label class="line-icon"
					:style="{'background': '#00c3c3'}"></label>修改{{obj.moneyTypeName}}报账</view>
			<u-button :loading="sb.load" type="success" @click="submitFm">
				<u-icon v-if="!sb.load" name="edit-pen-fill" color="#fff"
					:custom-style="{'margin-right': '10rpx'}"></u-icon>
				提交修改
			</u-button>
		</view>

		<u-form :model="fm.ps.qtjz" ref="qtjzfm">
			<view class="my-fm">
				<view class="f-item" :style="{'margin': '0rpx', 'padding': '0rpx'}">
					<u-form-item>
						<view class="i-row">
							<view class="r-title">{{obj.moneyTypeName}}金额</view>
							<view class="r-con">
								<u-input v-model="fm.ps.qtjz.reimMoney" :type="'digit'" :maxlength="6"
									:input-align="'right'" :clearable="false" :custom-style="{'color': '#00C3CE'}"
									:placeholder="'请输入金额'" /><label class="i-r-text">元</label>
							</view>
						</view>
					</u-form-item>
					<u-form-item>
						<view class="i-row-textarea">
							<view class="r-title">备注</view>
							<view class="r-con">
								<u-input v-model="fm.ps.qtjz.remark" :type="'textarea'"
									:custom-style="{'color': '#00C3CE'}" :placeholderStyle="'color: #f000000;'"
									:clearable="false" :auto-height="true" :height="150" :maxlength="200"
									:placeholder="'请填写备注，最多输入200个字（选填）'" />
							</view>
						</view>
					</u-form-item>
					<u-form-item :border-bottom="false">
						<view class="i-row-textarea">
							<view class="r-title">{{obj.moneyTypeName}}凭证<label class="r-title-tip">（最多上传6张图片）</label>
							</view>
							<view class="r-con">
								<u-upload ref="qtjzUf" width="200" height="200" max-count="6"
									:file-list="fm.ps.qtjz.uflist" :action="fm.ufAction" :header="fm.ufHeader"
									:name="'file'" :form-data="{ftype: 'BACK_SET_DATA_IMG',}"
									:max-size="1024*1024*30"></u-upload>
							</view>
						</view>
					</u-form-item>
				</view>
			</view>
		</u-form>
		<u-toast ref="qtoast" />
	</view>
</template>

<script>
	export default {
		
		data() {
			return {
				obj: {},
				plateNum: '',
				orderNum: '',
				// 维修详情输入框样式
				inpBtnStyl: {
					'color': '#00C3CE',
					'padding-right': '30rpx'
				},
				// 行程收支表单列表
				fmList: [],
				fm: {
					tcWayList: [],
					// 文件上传地址
					ufAction: this.C.upfileUrl,
					// 文件上传请求头信息
					ufHeader: {
						'token': uni.getStorageSync(this.C.uuid)
					},
					ps: {
						// 其他费
						qtjz: {
							// 记账类型
							jzType: 'QTJZ',
							id: '',
							reimMoney: '',
							reimVoucherUrl: "",
							moneyTypeId: "",
							remark: '',
							// 图片id
							fids: [],
							// 默认图片数组
							uflist: []
						},
					},


				},




				// 提交按钮
				sb: {
					load: false
				},


			}
		},
		onLoad(e) {
			const that = this,
				U = that.U;
			// 备份一份空的表单数据，方便清空表单
			for (let a in that.fm.ps) {
				let f = U.scopy(that.fm.ps[a]);
				// 存在记账类型属性才验证
				if (f.jzType) that.fmList.push(f);
			}
			console.log('传入的参数', e);


			that.getBzDetail(e.id);
		},
		methods: {



			// 获取-报账详情
			getBzDetail(id) {
				const that = this;

				that.$u.api.getOrderXcjzDetail({
					id: id
				}).then(res => {
					if (res.code == 1) {
						that.plateNum = res.data.plateNum
						that.orderNum = res.data.omsOrderNum
						that.obj = res.data
						let o = res.data;

						// 根据报账类型初始化对应数据
						let p = that.fm.ps.qtjz
						// 修改id
						p.id = o.id;
						// 车辆当前公里数
						p.reimMoney = o.reimMoney;

						// 备注
						p.remark = o.remark;
						
						// 图片id
						p.fids = [];
						// 默认图片数组
						let flist = [];
						if (o.reimVoucherUrl) {
							let fs = o.reimVoucherUrl.split(',');
							fs.forEach(it => {
								flist.push({

									url: 'https://cba360.com' + it
								});
							});
						}
						that.flist = flist; // 保存起来
						p.uflist = flist;


						console.log('赋值', p);
					}
				});
			},
			// 表单验证
			valFm() {
				const that = this,
					U = that.U,
					FV = that.FV,
					fm = that.fm.ps,
					staffReimInfo = [];
				let fg = true,
					valFmList = [];
				if (fg) {
					for (let b = 0; b < that.fmList.length; b++) {

					}

					that.fmList.forEach(it => {
						// 当前记账类型表单参数对象名称
						let jzt = it.jzType.toLowerCase();
						// 当前记账表单参数对象
						let currfm = U.scopy(fm[jzt]);

						// 新表单对象与基类表单对象不同，表示新表单存在新参数
						if (!that._.isEqual(it, currfm)) valFmList.push(it.jzType);
					});


				}
				// 只要有一个表单存在新参数，就需要验证
				if (valFmList.length > 0) {
					// 验证停车费报账表单
					if (fg && valFmList.findIndex(it => it == fm.qtjz.jzType) != -1) {
						let ps = U.scopy(fm.qtjz),
							p = {};
						if (fg) {
							p.jzType = ps.jzType;
							
							p.id = ps.id;
							if(that.obj.moneyTypeName == '过路费'){
								p.moneyTypeId = 12;
							}else if(that.obj.moneyTypeName == '打单费'){
									p.moneyTypeId = 13;
							}else if(that.obj.moneyTypeName == '生活费'){
									p.moneyTypeId = 14;
							}else if(that.obj.moneyTypeName == '住宿费'){
									p.moneyTypeId = 15;
							}else if(that.obj.moneyTypeName == '买水费'){
									p.moneyTypeId = 16;
							}else if(that.obj.moneyTypeName == '洗车费'){
									p.moneyTypeId = 17;
							}else if(that.obj.moneyTypeName == '其他费'){
									p.moneyTypeId = 19;
							}else if(that.obj.moneyTypeName == '打的费'){
									p.moneyTypeId = 20;
							}
						}
						if (fg) {
							let reimMoney = ps.reimMoney;
							if (!reimMoney || reimMoney == 0) {
								fg = U.qinfo('[金额]不能为空或0');
							} else if (!FV.isFloat(reimMoney)) {
								fg = U.qinfo('[金额]格式错误');
							} else {
								p.reimMoney = reimMoney;
							}
						}
						
						// if (fg) {
						// 	let remark = ps.remark;
						// 	if (remark.length > 200) {
						// 		fg = U.qinfo('[备注]最多填写200个字');
						// 	} else {
						// 		p.remark = remark;
						// 	}
						// }


						if (fg) {
							let uflist = that.$refs.qtjzUf.lists;
							if (uflist.length == 0) {
								fg = U.qinfo('至少需要上传一张小票图片');
							} else {
								let fids = [];
								uflist.forEach(it => {
									if (it.url && it.url.indexOf('_img') != -1) { // 本地图片
										let is = that.flist.find(fit => it.url.indexOf(fit.url) != -1);
										let url = is.url.split('.com')[1]
										console.log(url, 'url')
										fids.push(url);
									} else { // 新传图片
										if (it.response && it.response.data) fids.push(it.response.data.file.url);
									}
								});
								console.log(fids)
								if (fids.length == 0) {
									fg = U.qinfo('上传图片' + uflist.length + '张，成功' + fids.length + '张');
								} else {
									p.reimVoucherUrl = fids.join(',');
									staffReimInfo.push(p);
								}
							}
						}

					}

				}
				if (valFmList.length == 0) fg = U.qinfo('至少需要提交一项报账');

				return fg ? {
					staffReimInfo: staffReimInfo
				} : fg;
			},
			// 提交表单
			submitFm() {
				let that = this,
					U = that.U;

				let ps = that.valFm();
				if (ps != false) {

					ps.plateNum = that.plateNum
					ps.omsOrderNum = that.orderNum
					console.log('提交所有表单参数', JSON.stringify(ps));
					that.$u.api.adupOrderRouteBz(ps).then((res) => {
						if (res.code != 1) {
							U.qerror(res.msg);
						} else {
							console.log('结果', res);
							U.qinfo(res.msg);

							// 循环重置表单
							that.fmList.forEach(it => {
								// 当前记账类型表单参数对象名称
								let jzt = it.jzType.toLowerCase();

								let jzType = that.fm.ps[jzt].jzType;
								if (jzType) {
									that.fm.ps[jzt] = U.scopy(it);



									// 清空对应文件
									that.$refs[jzt + 'Uf'].clear();
								}
							});
							// 返回上一页
							uni.navigateBack({
								delta: 1
							});
						}
					});
				}
			},
			// 弹框列表选择
			mySeledMd(e) {
				console.log('父级接收子组件数据', e);
				const that = this,
					fm = that.fm.ps;

				that._.set(fm, e.name, e.obj);



				console.log('设置后的值', that.fm.ps);
			},

		}
	}
</script>

<style lang="scss">
	.adup-jybz {
		padding-top: 80rpx;
		padding-bottom: 10rpx;

		.f-item {
			margin: 10rpx !important;
		}
	}
</style>